import {
  IsNotEmpty,
  IsString,
  IsUrl,
  IsInt,
  Min,
  Max,
  Length,
  IsOptional,
  IsEnum,
  IsArray,
} from 'class-validator';
import { Transform } from 'class-transformer';

enum ContentType {
  DRAFT = 0,
  ARTICLE = 1,
  DIARY = 2,
}

enum OriginalType {
  ISORIGINAL = 0,
  NOTORIGINAL = 1,
}

export class CreateArticleDto {
  @IsNotEmpty({ message: '标题不能为空' })
  @IsString()
  @Length(1, 30, { message: '标题长度需在1-30字符之间' })
  title: string;

  @IsOptional() // 允许为 null 或 undefined
  @IsString()
  @Length(0, 100, { message: '摘要长度需在100字符之间' })
  synopsis?: string;

  @IsString()
  content: string;

  @IsOptional()
  @Transform(({ value }) => (value === '' ? null : value))
  @IsUrl({ protocols: ['http', 'https'], require_protocol: true })
  cover_img_url?: string | null;

  @IsInt()
  @Min(0)
  @Max(100000)
  words_num: number;

  @IsNotEmpty()
  @IsEnum(ContentType)
  type: number;

  @IsOptional()
  @IsArray()
  tags: number[];

  @IsNotEmpty()
  @IsEnum(OriginalType)
  original: number;
}
